/**
 * Created by HP on 2017/7/31.
 */
 function logout(){
	window.location.href="/i/noteuser/noteUser/noteUser";
}
 
window.onload=function(){
	var username=getCookie("userName");
	var userId=getCookie("userId");
    var check_all=document.getElementById("check_all");
    var checks=document.getElementsByClassName("check");
    var mytable=document.getElementById("mynote_table");
    var back_btn=document.getElementById("back_btn");
    var del_btn=document.getElementById("del_btn");

	var username=getCookie("userName");
	document.getElementById("user_name").innerHTML="您好，"+username;
	
	
    check_all.onclick=function(){
        for(var i=0;i<checks.length;i++)
        {
            checks[i].checked=this.checked;
        }
    }
    var request=new XMLHttpRequest();
    request.open("POST","/i/note/note/noteList", true);
    request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    var message="userId="+userId+"&type=2"+"&style=note_time_update";
    request.onreadystatechange=function(){
        if(request.readyState==4)
        {
            if(request.status==200)
            {
                var data=JSON.parse(request.responseText);
                for(var i=0;i<data.length;i++)
                {
                    var myrow=mytable.insertRow(1);
                    var w=myrow.insertCell(0);//复选框
                    var x=myrow.insertCell(1);//标题
                    var y=myrow.insertCell(2);//noteId
                    var z=myrow.insertCell(3);//日期
                    var w_check=document.createElement("input");
                    w_check.setAttribute("type","checkbox");
                    w_check.setAttribute("class","check");
                    w.appendChild(w_check);
                    x.innerHTML=data[i].noteTitle;
                    x.setAttribute("class","wrap");
                    var y_id=document.createElement("div");        //将noteId的值放在表格中，隐藏显示
                    y_id.setAttribute("style","display:block");
                    y_id.innerHTML=data[i].noteId;
                    y.appendChild(y_id);
                    z.innerHTML=data[i].noteTimeUpdate;
                }
            }
            else{
                alert("请求错误"+request.status);
            }
        }
    }
    request.send(message);

    back_btn.onclick=function(){             //恢复按钮
        alert("hello");
        for(var i=1;i<mytable.rows.length;i++)
        {
            if(mytable.rows[i].cells[0].firstChild.checked)
            {
                var id=mytable.rows[i].cells[2].firstChild.innerHTML;
               mytable.deleteRow(i);
                i--;                        //删除后i的值会变化
                var xmlhttp=new XMLHttpRequest();
                xmlhttp.open("POST","/i/note/note/update_type",true);
                xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                var send_data="userId="+userId+"&noteId="+id+"&type=1";
                xmlhttp.onreadystatechange=function(){
                    if(xmlhttp.readyState==4)
                    {
                        if(xmlhttp.status==200)
                        {
                            var data=JSON.parse(xmlhttp.responseText);
                            if(data.result==1)
                            {
                                alert("恢复成功");
                            }
                        }
                        else
                        {
                            alert("恢复错误"+xmlhttp.status);
                        }
                    }
                }
                xmlhttp.send(send_data);
            }
        }
    }
    //彻底删除按钮
    del_btn.onclick=function(){
        window.confirm("确定要彻底删除所选笔记吗？");
        for(var i=1;i<mytable.rows.length;i++)
        {
            if(mytable.rows[i].cells[0].firstChild.checked)
            {
                var id=mytable.rows[i].cells[2].firstChild.innerHTML;
                mytable.deleteRow(i);
                i--;

                var xmlhttp=new XMLHttpRequest();
                xmlhttp.open("POST","/i/note/note/deleteNote",true);
                xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                var send_data="userId="+userId+"&noteId="+id;
                xmlhttp.onreadystatechange=function(){
                    if(xmlhttp.readyState==4)
                    {
                        if(xmlhttp.status==200)
                        {
                            var data=JSON.parse(xmlhttp.responseText);
                            if(data.result==1)
                            {
                                alert("删除成功");
                            }
                        }
                        else
                        {
                            alert("删除错误"+xmlhttp.status);
                        }
                    }
                }
                xmlhttp.send(send_data);
            }
        }
    }

	function setCookie(cname,cvalue,exdays){
		var d = new Date();
		d.setTime(d.getTime()+(exdays*24*60*60*1000));
		var expires = "expires="+d.toGMTString();
		document.cookie = cname + "=" + cvalue + "; " + expires;
	} 


	function getCookie(cname){
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for(var i=0; i<ca.length; i++) {
			var c = ca[i].trim();
			if (c.indexOf(name)==0) return c.substring(name.length,c.length);
		}
		return "";
	} 
	
}